<template>
    <section>
        <!--工具条-->
        <el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
            <el-form :inline="true">
                <el-form-item >
                    <el-input
                            placeholder="请输入部门名称进行搜索..."
                            prefix-icon="el-icon-search"
                            v-model="filterText"
                            style="width: 495px">
                    </el-input>
                </el-form-item>
            </el-form>
        </el-col>

        <el-col :span="12">

            <el-tree
                    :data="deps"
                    :props="defaultProps"
                    :expand-on-click-node="false"
                    :filter-node-method="filterNode"
                    ref="tree">
            <span class="custom-tree-node" style="display: flex;justify-content: space-between;width: 100%;"
                  slot-scope="{ node, data }">
        <span>{{data.name }}</span>
        <span>
          <el-button
                  type="primary"
                  size="mini"
                  class="depBtn"
                  @click="() => showAddDepView(data)">
            添加部门
          </el-button>
          <el-button
                  type="danger"
                  size="mini"
                  class="depBtn"
                  @click="() => deleteDep(data)">
            删除部门
          </el-button>
        </span>
      </span>
            </el-tree>
            <el-dialog
                    title="添加部门"
                    :visible.sync="dialogVisible"
                    width="30%">
                <div>
                    <table>
                        <tr>
                            <td>
                                <el-tag>上级部门</el-tag>
                            </td>
                            <td>{{pname}}</td>
                        </tr>
                        <tr>
                            <td>
                                <el-tag>部门名称</el-tag>
                            </td>
                            <td>
                                <el-input v-model="dep.name" placeholder="请输入部门名称..."></el-input>
                            </td>
                        </tr>
                    </table>
                </div>
                <span slot="footer" class="dialog-footer">
    <el-button @click="dialogVisible = false">取 消</el-button>
    <el-button type="primary" @click="doAddDep">确 定</el-button>
  </span>
            </el-dialog>
        </el-col>
    </section>
</template>

<script>
    import {getDepts} from "@/api/api";
    export default {
        name: "DepMana",
        data() {
            return {
                dialogVisible: false,
                filterText: '',
                dep: {
                    name: '',
                    parentId: -1
                },
                pname: '',
                deps: [{
                    "id": 1, "name": "股东会", "parentId": -1, "depPath": ".1", "enabled": true, "children": [{
                        "id": 4, "name": "董事会", "parentId": 1, "depPath": ".1.4", "enabled": true, "children": [{
                            "id": 5,
                            "name": "总办",
                            "parentId": 4,
                            "depPath": ".1.4.5",
                            "enabled": true,
                            "children": [{
                                "id": 8,
                                "name": "财务部",
                                "parentId": 5,
                                "depPath": ".1.4.5.8",
                                "enabled": true,
                                "children": [],
                                "result": null,
                                "parent": false
                            }, {
                                "id": 78,
                                "name": "市场部",
                                "parentId": 5,
                                "depPath": ".1.4.5.78",
                                "enabled": true,
                                "children": [{
                                    "id": 81,
                                    "name": "华北市场部",
                                    "parentId": 78,
                                    "depPath": ".1.4.5.78.81",
                                    "enabled": true,
                                    "children": [{
                                        "id": 85,
                                        "name": "石家庄市场部",
                                        "parentId": 81,
                                        "depPath": ".1.4.5.78.81.85",
                                        "enabled": true,
                                        "children": [{
                                            "id": 105,
                                            "name": "44",
                                            "parentId": 85,
                                            "depPath": ".1.4.5.78.81.85.105",
                                            "enabled": true,
                                            "children": [],
                                            "result": null,
                                            "parent": false
                                        }],
                                        "result": null,
                                        "parent": true
                                    }],
                                    "result": null,
                                    "parent": true
                                }, {
                                    "id": 82,
                                    "name": "华南市场部",
                                    "parentId": 78,
                                    "depPath": ".1.4.5.78.82",
                                    "enabled": true,
                                    "children": [],
                                    "result": null,
                                    "parent": false
                                }, {
                                    "id": 86,
                                    "name": "西北市场部",
                                    "parentId": 78,
                                    "depPath": ".1.4.5.78.86",
                                    "enabled": true,
                                    "children": [{
                                        "id": 87,
                                        "name": "西安市场",
                                        "parentId": 86,
                                        "depPath": ".1.4.5.78.86.87",
                                        "enabled": true,
                                        "children": [{
                                            "id": 89,
                                            "name": "莲湖区市场",
                                            "parentId": 87,
                                            "depPath": ".1.4.5.78.86.87.89",
                                            "enabled": true,
                                            "children": [],
                                            "result": null,
                                            "parent": false
                                        }],
                                        "result": null,
                                        "parent": true
                                    }],
                                    "result": null,
                                    "parent": true
                                }],
                                "result": null,
                                "parent": true
                            }, {
                                "id": 91,
                                "name": "技术部",
                                "parentId": 5,
                                "depPath": ".1.4.5.91",
                                "enabled": true,
                                "children": [],
                                "result": null,
                                "parent": false
                            }, {
                                "id": 92,
                                "name": "运维部",
                                "parentId": 5,
                                "depPath": ".1.4.5.92",
                                "enabled": true,
                                "children": [{
                                    "id": 93,
                                    "name": "运维1部",
                                    "parentId": 92,
                                    "depPath": ".1.4.5.92.93",
                                    "enabled": true,
                                    "children": [],
                                    "result": null,
                                    "parent": false
                                }, {
                                    "id": 94,
                                    "name": "运维2部",
                                    "parentId": 92,
                                    "depPath": ".1.4.5.92.94",
                                    "enabled": true,
                                    "children": [],
                                    "result": null,
                                    "parent": false
                                }],
                                "result": null,
                                "parent": true
                            }],
                            "result": null,
                            "parent": true
                        }], "result": null, "parent": true
                    }, {
                        "id": 96,
                        "name": "bbb",
                        "parentId": 1,
                        "depPath": ".1.96",
                        "enabled": true,
                        "children": [{
                            "id": 104,
                            "name": "111",
                            "parentId": 96,
                            "depPath": ".1.96.104",
                            "enabled": true,
                            "children": [],
                            "result": null,
                            "parent": false
                        }],
                        "result": null,
                        "parent": true
                    }], "result": null, "parent": true
                }],
                defaultProps: {
                    children: 'children',
                    label: 'name'
                }
            }
        },
        watch: {
            filterText(val) {
                this.$refs.tree.filter(val);
            }
        },
        mounted() {
            this.initDeps();
        },
        methods: {
            initDep() {
                this.dep = {
                    name: '',
                    parentId: -1
                }
                this.pname = '';
            },
            addDep2Deps(deps, dep) {
                for (let i = 0; i < deps.length; i++) {
                    let d = deps[i];
                     if ("undefined" == typeof dep.parentId) {
                         //alert("dkddkkdkdkd")
                     }
                    if (d.id == dep.parentId) {
                        d.children = d.children.concat(dep);
                        if (d.children.length > 0) {
                            d.parent = true;
                        }
                        return;
                    } else {
                        //this.addDep2Deps(d.children, dep);
                    }
                }
            },
            doAddDep() {
                this.postRequest("/org/dept/add/", this.dep).then(resp => {
                    if (resp) {
                      //  this.addDep2Deps(this.deps, resp.obj);
                        this.dialogVisible = false;
                        //初始化变量
                        this.initDeps();
                    }
                })
            },
            removeDepFromDeps(p, deps, id) {
                for (let i = 0; i < deps.length; i++) {
                    let d = deps[i];
                    if (d.id == id) {
                        deps.splice(i, 1);
                        if (deps.length == 0) {
                            p.parent = false;
                        }
                        return;
                    } else {
                        this.removeDepFromDeps(d, d.children, id);
                    }
                }
            },
            deleteDep(data) {
                if (data.parent) {
                    this.$message.error("父部门删除失败");
                } else {
                    this.$confirm('此操作将永久删除【' + data.name + '】部门, 是否继续?', '提示', {
                        confirmButtonText: '确定',
                        cancelButtonText: '取消',
                        type: 'warning'
                    }).then(() => {
                        this.deleteRequest("/org/dept/delete/" + data.id).then(resp => {
                            if (resp) {
                                this.removeDepFromDeps(null, this.deps, data.id);
                            }
                        })
                    }).catch(() => {
                        this.$message({
                            type: 'info',
                            message: '已取消删除'
                        });
                    });
                }
            },
            showAddDepView(data) {
                this.pname = data.name;
                this.dep.parentId = data.id;
                this.dialogVisible = true;
            },
            initDeps() {
                getDepts().then(res => {
                    //自己的业务逻辑代码：服务端返回的参数都封装在了data里面
                    this.deps=res.data;
                    console.log(res.data);
                    console.log(this.deps);
                }).catch(e => {
                    console.log("请求错误");
                    console.log(e);
                    //自己的业务逻辑代码：服务端返回的异常都封装在了e里面
                });
            },
            filterNode(value, data) {
                if (!value) return true;
                return data.name.indexOf(value) !== -1;
            }
        }
    }
</script>

<style scoped>
    .depBtn {
        padding: 2px;
    }

    .el-button--mini, .el-button--mini.is-round{
        padding: 3px  2px !important;
    }
</style>
